<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" @loaded="loaded" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				busCode:"",
				InstCacheKey:'fin-js-hourWage-inst-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			loaded(){
				let _that = this
				if(!_that.$refs.autoform.formdata.zposId){
					_that.$refs.autoform.initSelectVal("topinfo","zposId")
				}
			},
			getUser(id){
				let _that = this
				_that.om.request(
					"tposition/getPrimary",
					{
						instId:id
					}
				).then(res=>{
					_that.$refs.autoform.formdata.userId = res.value.userId
					_that.$refs.autoform.formdata.userName = res.value.userName
				})
			},
			onInputOverOrder(value){
				 let str = newValue.replace(/[\u4E00-\u9FA5\uF900-\uFA2D]/g,'');
				 if(value != str){
					 _that.$refs.autoform.overOrderCodes = value
				 }
			},
			formSubmit(){
				let _that = this 
				if(_that.checkedit){
					_that.om.formSave("commonForm/doCheckOrApproveUpdate",_that.formdata,_that)
				}else if(_that.edit){
					_that.om.formSave("hourWage/doUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("hourWage/doAdd",_that.formdata,_that)
				}
				
			},
			verifyInst(){
				let _that = this
				if(!_that.$refs.autoform.formdata.zposId){
					_that.om.toast('请先选择制表人岗位',3000)
					return false
				}else{
					return true
				}
			},
			
			onSelectZpos(item){
				this.getPostList(item.id)
			},
			getPostList(id){
				let _that = this
				_that.$refs.autoform.changeEditor("title1","instId",{
					link:"../../public/pageSearch/pageSearch",
					"linkParam":{
						other:JSON.stringify({
							businessCode:_that.busCode,
							postId: id
						}),
						namefield: 'instName',
						searchfield: "q",
						initload: true,
						url:'tinstitution/findInstList',
						cachename: _that.InstCacheKey,
						placeholder: "请输入部门名称"
					}
				})
			},
			onSelectInst(item){
				this.getInstList(item.id)
			},
			verifyDataStr(){
				let _that = this
				if(_that.$refs.autoform.formdata.instId){
					return true
				}else{
					_that.om.toast('请先选择部门名称',3000)
					return false
				}
				
			},
			getWorkParam(){
				let _that = this
				return{
					instId:_that.$refs.autoform.formdata.instId
				}
			},
			intLoad(options) {
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				let fileId = options.fileId
				_that.busCode = options.code
				if(fileId){
					ajaxArr.push({
						url:"hourWage/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo", "zposId", "data", configSelectData(res[0], "id", "posname"), mainData)
					if(_that.subEditorObj["title2"]){
						changeEditor("title2","dataStr",{
							getWorkParam:"getWorkParam",
							titleTemplate:"<%= '姓名:' %> <%= workUserName || '未填写' %>",
							subtitle1Template:"<%='工作内容:'%>  <%= workContent || '未填写' %>",
							subtitle2Template:"<%='申请工时:'%> <%= applyHours || '未填写' %>" +"|"+"<%='小时工资(元/h):'%> <%= hourPrice || '未填写' %>",
							subtitle3Template:"<%= '金额(元):'%><%= totalPrice || '未填写' %>"
						},null,mainData)
					}
					if(_that.subEditorObj["title3"]){
						changeEditor("title3","reviewerStr",{
							titleTemplate:"姓名:<%= userName %>",
							subtitle1Template:"岗位:<%= postName %>"
						},null,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[1].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value[index])
								_that.$set(_that.formdata,index,res[1].value[index])
							}
						}
						if(_that.subEditorObj["title2"]){
							let dataList = _that._.cloneDeep(res[1].value.dataList)
							_that.subEditorObj["title2"].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(dataList))
							_that.$set(_that.formdata,"dataStr",JSON.stringify(dataList))
							delete res[1].value.dataList
						}
						if(_that.subEditorObj["title3"]){
							let reviewerList = _that._.cloneDeep(res[1].value.reviewerList)
							_that.subEditorObj["title3"].workList = reviewerList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"reviewerStr",JSON.stringify(reviewerList))
							_that.$set(_that.formdata,"reviewerStr",JSON.stringify(reviewerList))
							delete res[1].value.reviewerList
						}
						_that.$nextTick(function(){
							_that.getPostList(_that.$refs.autoform.formdata.zposId)
						})
					}
				}, options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.intLoad(options)
		},
		onUnload(){
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							for(let item of res.data){
								if(!item.fileList){
									item.fileList = new Array()
								}
							}
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.InstCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.instId = d.id
							formdata.instName = d.instName
							_that.getUser(d.id)
							uni.removeStorage({
								key:_that.InstCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
